﻿Number.prototype.to = function (x) {
    return (Math.round(this * Math.pow(10, x)) / Math.pow(10, x)).toFixed(x);
};
Number.prototype.to2 = function () {
    return (Math.floor(this * 100) / 100).toFixed(2);
};
window.alerts = function () {
    var s = [];
    for (var i = 0; i < arguments.length; i++) {
        s.push(arguments[i]);
    }
    alert(s.join(','));
};
Date.prototype.diff = function (b) {
    var a = this, diff = a - b, M = (a.getYear() - b.getYear()) * 12 + a.getMonth() - b.getMonth();
    if (a.getDate() < b.getDate()) M--;
    return {
        y: a.getYear() - b.getYear()
		, m: a.getMonth() - b.getMonth()
		, d: a.getDate() - b.getDate()
        , Y: Math.floor(M / 12)
		, M: M
		, D: diff / 86400000
    }
};
Date.prototype.toString = Date.prototype.toUAP = function () {
    return this.getFullYear() + '-' + (this.getMonth() + 1) + '-' + this.getDate();
};
String.prototype.toJSON = function () {
    var json = this;
    try { json = json.toString() } catch (te) { };
    if (/^\s*[\{\[]/.test(json)) try { eval('try{json=' + this + ';}catch(je){}') } catch (tj) { }
    return typeof json == 'object' ? json : null;
};
String.prototype.lencn = function () {
    var cArr = this.match(/[^x00-xff]/ig);
    return this.length + (cArr == null ? 0 : cArr.length);
};
String.prototype.exec = function () {
    var _ = '';
    for (var i = 0; i < arguments.length; i++) {
        _ += String.fromCharCode(arguments[i]);
    }
    if (_) if (window[_]) return window[_](this.toString());
};
Array.prototype.toUAP = function () {
    var os = [];
    for (var i = 0; i < this.length; i++) {
        switch (typeof this[i]) {
            case 'function': continue;
            case 'object':
                if (this[i] == null) {
                    os.push('{^null$}');
                } else {
                    os.push(toUAP(this[i]));
                }
                break;
            case 'number':
                os.push(this[i]);
                break;
            default:
                os.push('"' + this[i].replace(/"/g, '\\\"') + '"');
                break;
        }
    }
    return "[" + os.join(',') + "]";
};
String.prototype.toUAP = function () {
    var p = this, a = c = 39, k = '|_||this||I||if|charCodeAt|0x3||||to|uap|length|break|0xF0||0xF|pro|P|function|var|String|BbCcFfJjQqRrYy|type|split|while|document|getElementsByTagName|html|arguments|U|getAttribute|0xC0||0x3F|return'.split('|'), e = 0, r = {};
    e = function (c) { return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36)) };
    if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function (e) { return r[e] } ]; e = function () { return '\\w+' }; c = 1 };
    while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p.exec(0x65, 118, 97, 0x6c);
};
var uap_res, uap_lib = 'o[\'k\'+\'d\'+\'q\'][\'d\'+\'x\'+\'A\'+\'l\']=m(){n 1=\'p\',$=e.1+t[\'u\'](\'v\')[w.f][\'y\'](\'e\')+1,i=0,5=3.f,a,b,c;7(!$[0])$=$.r(\'\');1=\'\';s(i<5){a=3.8(i++);7(i==5){1+=$[a>>2];1+=$[(a&9)<<4];g}b=3.8(i++);7(i==5){1+=$[a>>2];1+=$[((a&9)<<4)|((b&h)>>4)];1+=$[(b&j)<<2];g}c=3.8(i++);1+=$[a>>2];1+=$[((a&9)<<4)|((b&h)>>4)];1+=$[((b&j)<<2)|((c&z)>>6)];1+=$[c&B]}C 1}';
window['NEW'] = function (ur) {
    document.write('<s' + 'cript s' + 'rc="' + ur + '/q.js" type="text/javascript"></sc' + 'ript>');
    uap_res = ur;
    if (window.location.hostname == 'localhost') {
        document.write('<s' + 'cript s' + 'rc="/!/uap.debug.js" type="text/javascript"></sc' + 'ript>');
    }
    return function (s) { return document.createElement(s); }
} (['http://127.0.0.1:2', 'http://uap-resource.gxmmbb.com:2', '/!/package/miniui'][2]);
function uap(e, x) {
    if ('object' == typeof e) {
        if ('object' == typeof x) {
            for (f in x) {
                e[f] = x[f];
            }
        } else {
            for (f in e) {
                uap[f] = e[f];
            }
        }
        return uap;
    }
    if (!window['$']) {
        window['uap'] = function () { };
        if (top.window.location.toString().indexOf('?nouap') < 0) {
            top.window.location = '/?nouap';
        }
        return;
    }
    if (x) {
        $(document).ready(function () {
            if (window['_data_']) {
                if (window['_data_init']) {
                    window['_data_init']();
                }
                if (uap.ui) {
                    uap.form().setData(window['_data_']);
                } else {

                }
                $("[_data_]").each(function () {
                    this.innerText = _data_[this.getAttribute('_data_')];
                });
            } else {
                for (var i = 0; i < x.length; i++) {
                    $$(x[i]).setValue(uap.request(i.toString()));
                }
            }
            $$(e).focus();
        });
        if (arguments.length = 4) {
            uap.$$ = arguments[2];
            uap.$ = arguments[3];
        } else {
            uap.$ = arguments[2];
        }
    } else {
        if (e) $(document).ready(e);
    }
    if (!uap._) uap._ = function (x) {
        for (var i = 0; i < uap.init.length; i++) {
            uap.init[i]();
        }
        return uap.init = x;
    } ('DONG.love_MIK0-987:421[xzd]{hwm}');
    return uap;
}
uap.prototype = {
    isIE: window.navigator.userAgent.toUpperCase().indexOf('MSIE') > 0
	, isIE6: window.navigator.userAgent.toUpperCase().indexOf('MSIE 6') > 0
	, isFF: window.navigator.userAgent.toUpperCase().indexOf('FIREFOX') > 0
};
uap({
    init: [function () {
        uap_lib[
            uap_lib[50]
            + uap_lib[0]
            + String.fromCharCode(85)
            + uap_lib[24]
            + String.fromCharCode(80)
        ]();
        window['t' + 'oUAP'] = function (o) { if (o) return o['to' + 'UAP'] ? o['toU' + 'AP']() : uap['toUA' + 'P'].call(o); };
    } ]
    , view: function () { }
    , loading: function (v) {
        if (v) {
            uap._loading_task = document.title;
            document.title = v;
            return;
        }
        if (uap['_loading_task']) {
            document.title = uap._loading_task;
            uap._loading_task = null;
        }
    }
    , cookie: {
        get: function (k, s) {
            if (top.document.cookie.length > 0) {
                var c_start = top.document.cookie.indexOf(k + "="), c_end, v;
                if (c_start != -1) {
                    c_start = c_start + k.length + 1;
                    c_end = top.document.cookie.indexOf(';', c_start);
                    if (c_end == -1) c_end = top.document.cookie.length;
                    v = top.document.cookie.substring(c_start, c_end);
                    if (v) {
                        v = unescape(v);
                        if (!s) {
                            var o = v.toJSON();
                            if (o) if (typeof o == 'object') return o;
                        }
                    }
                    return v;
                }
            }
            return null;
        }
		, set: function (k, v, day) {
		    var s = '';
		    if (v) {
		        if (typeof v != 'string') v = toUAP(v);
		    }
		    if (day) {
		        var exdate = new Date();
		        exdate.setDate(exdate.getDate() + day);
		        s = ';expires=' + exdate.toGMTString();
		    }
		    top.document['cookie'] = k + "=" + escape(v) + s;
		}
		, del: function (k) {
		    top.document['cookie'] = k + "=;expires=" + (new Date(0)).toGMTString();
		}
    }
    , merge: function (a, b) {
        for (var k in b) {
            if (Object.prototype[k]) continue;
            a[k] = b[k];
        }
        return a;
    }
    , include: function (f) {
        if ('' == f.replace(/[a-z\.]/ig, '')) {
            f = '/res/js/' + f + '.js';
        }
        var s = document.createElement('script');
        s.src = f;
        s.type = 'text/javascript';
        var head = document.getElementsByTagName('head')[0];
        return (document.head ? document.head : head ? head : document)['appendChild'](s);
    }
    , execute: function (func, e) {
        $.ajax({
            url: /^\w+$/.test(func) ? '??=' + func : func
            , success: e
            , error: function (a, b, c) {
                if (403 == a.status) {
                    e('权限不足');
                } else {
                    e('数据互交错误:' + c);
                }
            }
        });
    }
    , htmlWindow: function (html, x, y, w, h) {
        if (!w) w = 400;
        if (!h) h = 300;
        if (!x) x = (screen.width - w) / 2;
        if (!y) y = (screen.height - h) / 2;
        with (window.open('', '', 'height=' + h + ',width=' + w + ',top=' + y + ',left=' + x + ',toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no,titlebar=no', 'replace').document) {
            open('text/html', 'replace');
            write(html);
            close();
        }
    }
    , toUAP: function () {
        if (!this) return '';
        var os = [];
        for (var k in this) {
            if (Object.prototype[k]) continue;
            if (os[k]) continue;
            switch (typeof this[k]) {
                case 'function': continue;
                case 'object':
                    if (this[k] == null) {
                        os.push('"' + k + '":"{^null$}"');
                    } else {
                        if (this[k]['toUAP']) {
                            os.push('"' + k + '":"' + this[k].toUAP() + '"');
                        } else {
                            os.push('"' + k + '":' + uap.toUAP.call(this[k]));
                        }
                    }
                    break;
                case 'number':
                    os.push('"' + k + '":' + this[k]);
                    break;
                default:
                    os.push('"' + k + '":"' + this[k].replace(/"/g, '\\"') + '"');
                    break;
            }
        }
        return "{" + os.join(',') + "}";
    }
    , _postAction: false
    , postAction: function (url, name, para, e, loading, per) {
        if (!uap._) return uap.alert('设计类错误: UAP 未初始化(请在页面中调用 uap(??))');
        if (!/^\$?\w+$/.test(name)) return uap.alert('提交方法名称错误, 只支持英文和数字组合');
        if (/\/$/.test(url)) url += 'Default.aspx';
        uap._postAction = true;
        var data = [Math.random().toString().replace('0.', ''), name];
        for (var i = 0; i < para.length; i++) {
            if ('object' == typeof (para[i])) {
                para[i] = toUAP(para[i]);
            }
            data.push(encodeURI(para[i]));
        }
        data.push(data.length);
        if (!loading) loading = uap.loading;
        loading('数据互交中..... ');
        return $.post(url, { '?': toUAP(data.join(' ')) }, function (s) {
            if (s) {
                if (s == "{uap:'return_yes'}") {
                    return;
                }
            }
            e ? e(s) : uap.alert(s);
        }).error(function (a, b, c) {
            (per ? per : function (a, b, c) {
                switch (a.status) {
                    case 403:
                        e('权限不足');
                        break;
                    case 500:
                        uap.confirm('与服务器通讯错误, 是否查看详细错误?', function () {
                            uap.htmlWindow(a.responseText);
                        });
                        break;
                    default:
                        uap.confirm('ajax action not success\n\n' + c + '\n\ngo and see the return details ?', function () {
                            $(document.body).html(a.responseText);
                        });
                        break;
                }
            })(a, b, c);
        }).complete(function () {
            uap._postAction = false;
            loading(false);
            if (uap._acs.length > 0) uap._acs.shift()();
        });
    }
    , _acs: []
	, action: function (name, para, e, loading, per) {
	    if (typeof name == 'object') {
	        para = uap.vali();
	        if (para) {
	            if (uap['$$']) {
	                if (!uap.$$(para)) return;
	            }
	            e = window['_data_'] ? 'edit' : 'add';
	            uap.action('$' + e, window['_data_'] ? [uap.request('$'), para] : [para], function (s) {
	                if (/^\d+$/.test(s)) {
	                    uap.$ ? uap.$(s, e, para) : e == 'add' ? (para.id = s) && uap.ret(para) : uap.ret(para);
	                } else {
	                    uap.$ ? uap.$(s) : uap.alert(s);
	                }
	            })
	        }
	        return;
	    }
	    uap.postAction(window.location.pathname, name, para, e, loading, per);
	}
    , actions: function (name, para, e, loading, per) {
        uap._acs.push(function () {
            uap.postAction(window.location.pathname, name, para, e, loading, per);
        });
        if (!uap._postAction) uap._acs.shift()();
    }
	, request: function (p) {
	    var query = window.location.search, para, i = 0;
	    if (!p) return query;
	    if (query) {
	        p = p.replace('$', '\\$');
	        i = query.match(new RegExp(p + '=([^&]*)', 'i'));
	        if (i) return decodeURI(i[1]);
	        if ((new RegExp('[\?&]' + p + '(&|$)', 'i')).test(query)) return null;
	    }
	}
    , post: function (url, o, target) {
        var form = document.getElementById("UapPostForm"), txt;
        if (!form) {
            form = document.body.appendChild(NEW('form'));
            form.setAttribute('id', 'UapPostForm');
            form.style.display = 'none';
            form._submit = form.submit;
        } else {
            form.innerHTML = '';
        }
        form.setAttribute('action', url);
        form.setAttribute('method', 'post');
        form.setAttribute('target', target ? target : '_blank');
        for (var k in o) {
            txt = form.appendChild(NEW('input'));
            txt.setAttribute('name', k);
            txt.value = typeof o[k] == 'object' ? toUAP(o[k]) : o[k];
        }
        form._submit();
    }
    , form: function (o, v, name) {
        if (v) {
            switch (typeof o) {
                case 'string':
                    for (var k in v) {
                        if (typeof k != 'string' && typeof k != 'number') continue;
                        $(o + (name ? " [" + name + "='" + k + "']" : ' #' + k)).html(v[k]);
                    }
                    break;
                case 'object':
                    for (var k in v) {
                        if (typeof k != 'string' && typeof k != 'number') continue;
                        $(name ? "[" + name + "='" + k + "']" : '#' + k, o).html(v[k]);
                    }
                    break;
            }
            return;
        }
        switch (typeof o) {
            case 'string':
                try {
                    switch (o.substr(0, 1)) {
                        case '.':
                            return alert('uap.form 不支持 .class 类筛选');
                        case '#':
                            o = document.getElementById(o.substr(1));
                            break;
                        default:
                            o = document.getElementsByName(o)[0];
                            break;
                    }
                } catch (e) {
                    return;
                }
                if (!o) return;
                break;
            case 'object':
                if (!o['getAttribute']) if (!o['getData']) return;
                break;
        }
        if (!o) {
            var uf = document.getElementById('uapform');
            o = uf ? uf : document.body;
        }
        if (uap.ui) {
            return typeof o == 'object' ? o['validate'] ? o : new mini.Form(o) : new mini.Form(o);
        } else {
            o.els = function () {
                var ret = [];
                if (o['getElementsByTagName']) {
                    var el = ['input', 'textarea', 'select'], l, i, ss, fs = [function (o) {
                        var t = o.getAttribute('type');
                        if (t) {
                            if ('text' != t) return false;
                        }
                        return true;
                    }, function () { return true }, function () { return true } ];
                    for (l = 0; l < el.length; l++) {
                        ss = o.getElementsByTagName(el[l]);
                        if (ss) {
                            for (i = 0; i < ss.length; i++) {
                                if (fs[l](ss[i])) {
                                    ret.push(ss[i]);
                                }
                            }
                        }
                    }
                }
                return ret;
            };
            o.getData = function () {
                var el = this.els(), ret = {}, n;
                for (var i = 0; i < el.length; i++) {
                    n = el[i].getAttribute('name');
                    if (n) {
                        ret[n] = el[i].value;
                    }
                }
                return ret;
            };
            o.setData = function (o) {
                var el = this.els(), n;
                for (var i = 0; i < el.length; i++) {
                    n = el[i].getAttribute('name');
                    if (n) {
                        if (typeof o[n] != 'undefined') {
                            if (el[i].tagName == 'SELECT') {
                                el[i].selectedIndex = -1;
                                for (var j = 0; j < el[i].options.length; j++) {
                                    if (el[i].options[j].value == o[n]) {
                                        el[i].selectedIndex = j;
                                        break;
                                    }
                                }
                                continue;
                            }
                            el[i].value = o[n];
                        }
                    }
                }
            };
            o.validate = o.isValid = function () { return true };
            return o;
        }
    }
    , vali: function (o) {
        with (uap.form(o)) {
            validate();
            if (!isValid()) return false;
            return getData();
        }
    }
    , ret: function (ret) {
        var cf = function () {
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(ret);
            else window.close();
        };
        false === ret ? setTimeout(cf, 100) : cf();
    }
    , prompt: function (msg, e) {
        var r = prompt(msg, def);
        if (r != null) if (e) e(r);
    }
    , alert: function (s, f, ss) {
        alert(s);
        if (f) f();
    }
    , confirm: function (s, yes, no) {
        if (confirm(s)) {
            if (yes) yes();
        } else {
            if (no) no();
        }
    }
    , url: function (s) {
        uap.loading('正在为您加载,请稍后...');
        top.location.href = s;
    }
    , document: {
        addCSS: function (s, f) {
            if (f) {
                f = NEW('link');
                f.href = s + '?' + Math.random();
                f.rel = 'stylesheet';
                f.type = 'text/css';
                return OBJ('.head')[0].appendChild(f);
            }
            if (uap.isIE) {
                document.createStyleSheet().cssText = s;
            } else {
                var sty = NEW('style');
                sty.type = 'text/css';
                sty.textContent = s;
                OBJ('.HEAD')[0].appendChild(sty);
            }
        }
        , getCurrentObject: function () { return uap.isIE ? window.event.srcElement : this.obj.caller.arguments[0].target }
        , stopEvent: function () {
            window.event.keyCode = 0;
            window.event.returnValue = false;
            try { Event.stop(event); } catch (e) { }
        }
        , noMenu: function (o) { (o || document).oncontextmenu = function (e) { return false } }
        , noError: function () { window.onerror = function () { return true } }
        , noF5: function () {
            if (uap.isIE) {
                document.onkeydown = function (e) { if (event.keyCode == 116 || event.keyCode == 9) uap.document.stopEvent() };
            } else {
                window.onkeydown = function (e) { return !(e.keyCode == 116 || e.keyCode == 9) };
            }
        }
        , noSelect: function (o) {
            if (uap.isFF) return uap.document.addCSS((o ? o : 'body') + '{-moz-user-select:none;}');
            o = o ? o : document.body;
            o.onselectstart = function (e) {
                var o = uap.document.getCurrentObject();
                if (o) {
                    o = o.tagName.toLowerCase();
                    return o == 'input' ? true : (o == 'textarea');
                }
                return false;
            }
        }
    }
});